/**
 * 
 * @author  phoenics@126.com
 * @version v.0.1
 * @date  2018年6月17日 下午7:05:35 
 * 
 */
package phoenics.common.interceptor;

import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

import phoenics.common.BaseData.RestResp;
import phoenics.common.util.JacksonUtil;

/**
 *
 */
//@RestControllerAdvice
public class ResultResponse  implements ResponseBodyAdvice<Object> {
	private static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(ResultResponse.class);

	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice#supports(org.springframework.core.MethodParameter, java.lang.Class)
	 */
	@Override
	public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
		// TODO Auto-generated method stub
		return true;
	}

	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice#beforeBodyWrite(java.lang.Object, org.springframework.core.MethodParameter, org.springframework.http.MediaType, java.lang.Class, org.springframework.http.server.ServerHttpRequest, org.springframework.http.server.ServerHttpResponse)
	 */
	@Override
	public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
			Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request,
			ServerHttpResponse response) {
		if(body instanceof RestResp ) {
			return body;
		}
		Object res=RestResp.success(body);
		if(body instanceof String) {
			return  JacksonUtil.toJSon(res);
		}
		return res;
	}

}
